//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2006-2007
// (c) Paul Lougovskov (petrovich_73@mail.ru), 2005
//
// Road class visualization.
//

# include "StdAfx.h"
# include "Resource.h"
# include "DrawRoadClass.h"
# include "Types.h"
# include "Map.h"

# pragma const_seg (".text")

const COLORREF c_colRoadSpeedColors [8] = {
	RGB (250,  56,  71), // 5 kph or not for car, Red
	RGB (255, 160,  20), // 20 kph, dark yellow
	RGB (255, 210,  20), // 40 kph, yellow
	RGB ( 84, 239,  56), // 60 kph, green
	RGB ( 80, 240, 239), // 80 kph, blue
	RGB ( 46, 158, 204), // 90 kph, light blue
	RGB ( 42,  64, 188), // 110 kph, dark blue
	RGB (243,  98, 181)  // no speed limit, violett
};

const COLORREF c_colNotRoad = RGB (180, 180, 180);
const COLORREF c_colRoad    = RGB (255,  25,  25);

# pragma const_seg ()

static HPEN g_hpenNotRoad;
static HPEN g_hpenRoad;
static HPEN g_hpenRoad3b;
static HPEN g_hpenRoad4b;

void InitRoadClasses () {
	g_hpenNotRoad = ::CreatePen (PS_SOLID, 1, c_colNotRoad);
	g_hpenRoad    = ::CreatePen (PS_SOLID, 1, c_colRoad);
	g_hpenRoad3b  = ::CreatePen (PS_SOLID, 5, RGB (130, 130, 130));
	g_hpenRoad4b  = ::CreatePen (PS_SOLID, 6, RGB (0, 0, 0));
}

void DestroyRoadClasses () {
	::DeleteObject (g_hpenNotRoad);
	::DeleteObject (g_hpenRoad);
	::DeleteObject (g_hpenRoad3b);
	::DeleteObject (g_hpenRoad4b);
}

void DrawRoadClass (HDC _hDC, const rgn_t & _rgn, POINT * _pts, const size_t _cPoints) {
	const bool bIsRoad = g_map.pTypeSet->pfIsRoad (_rgn);
	if (bIsRoad && _rgn.pExtra) {
		int nRoadSpeedColor = c_colRoadSpeedColors [_rgn.pExtra->fSpeedLimit];
		if (_rgn.pExtra->bNoCar)
			nRoadSpeedColor = c_colRoadSpeedColors [0];

		int nRoadThickness = _rgn.pExtra->fRouteClass + 1; 
		if (nRoadThickness == 4) {
			::SelectObject (_hDC, g_hpenRoad3b);
			::Polyline (_hDC, _pts, _cPoints);	
			nRoadThickness = 3;
		} else if (nRoadThickness == 5) {
			::SelectObject (_hDC, g_hpenRoad4b);
			::Polyline (_hDC, _pts, _cPoints);
			nRoadThickness = 3;
		}

		const HPEN hpenRoad = ::CreatePen (PS_SOLID, nRoadThickness, nRoadSpeedColor);

		const HPEN hPenPrev = (HPEN) ::SelectObject (_hDC, hpenRoad);
		::Polyline (_hDC, _pts, _cPoints);

		::SelectObject (_hDC, hPenPrev);
		::DeleteObject (hpenRoad);
	} else {
		::SelectObject (_hDC, bIsRoad ? g_hpenRoad : g_hpenNotRoad);
		::Polyline (_hDC, _pts, _cPoints);
	}
}
